package com.i2bt.adapter;

import com.i2bt.R;
import com.i2bt.app.friend.FriendFragment;
import com.i2bt.bean.FriendInfoBean;
import com.i2bt.util.RandomImageUtil;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class FriendListAdapter extends BaseAdapter{
	
	private FriendFragment friendFragment;		//好友碎片
	
	private List<FriendInfoBean> friendList;		//好友的信息链表
	
	public FriendListAdapter(FriendFragment friendFragment, List<FriendInfoBean> friendList) {
		this.friendFragment = friendFragment;
		
		if (friendList == null) {
			this.friendList = new ArrayList<FriendInfoBean>();
		}
		else {
			this.friendList = friendList;
		}
	}

	@Override
	public int getCount() {
		return friendList.size();
	}

	@Override
	public Object getItem(int position) {
		return friendList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(friendFragment.getActivity())
					.inflate(R.layout.item_friend_list, null);
			
			holder = new ViewHolder();
			holder.friendName = (TextView) convertView.findViewById(R.id.friend_item_name);
			holder.friendImage = (ImageView) convertView.findViewById(R.id.friend_item_img);
			
			convertView.setTag(holder);
		}
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		
		holder.friendImage.setImageDrawable(RandomImageUtil.
				getFriendImg(friendFragment.getActivity()));
		
		FriendInfoBean friendBean = (FriendInfoBean) friendList.get(position);
		holder.friendName.setText(friendBean.getFriendName());
		
		
		return convertView;
	}
	
	private class ViewHolder {
		
		ImageView friendImage;
		
		TextView friendName;
	}
	
	public void setData(List<FriendInfoBean> friendInfoList) {
		this.friendList = friendInfoList;
	}

}
